﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using System.Data;
using Jumpton.Data;

namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class ProductManager : BusinessLogic
    {
        public PRODUCTS Load(int ID)
        {
            return (PRODUCTS)ObjectRow.Load(typeof(PRODUCTS), string.Format("this.ID={0}", ID));
        }

        public ObjectTable GetProductByOrderNo(decimal domainID)
        {
            return new ObjectTable(typeof(PRODUCTVIEW), string.Format("a.DOMAIN_ID={0} and a.ACTIVE = '1'", domainID));
        }

        public DataSet GetProductByUserID(string domainID, string user_id, string updatetime)
        {
            string strSql = string.Format("select a.*, b.DENOMINATOR, f.NAME as FORMNAME,n.* from PRODUCTS a \n"
            + "join PRODUCT_UOMS b  \n"
            + "on b.PRODUCT_ID = a.ID \n"
            + "join \n"
            + "(select d.PRODUCT_ID,d.FLAG,d.case_price as caseprice,d.piece_price as pieceprice  from CUSTOMER_PROD_LIST_ITEMS d \n"
            + "join (select distinct c.CUSTOMER_PRODUCT_LIST_ID \n"
            + "from CUSTOMERS c \n"
            + "Inner join ROUTE_CUSTOMERS e \n"
            + "on e.CUSTOMER_ID = c.ID \n"
            + "where e.USER_ID={0} and e.ROUTE_NUMBER=0) m \n"
            + "on d.CUSTOMER_PRODUCT_LIST_ID = m.CUSTOMER_PRODUCT_LIST_ID \n"
            + "group by d.PRODUCT_ID,d.FLAG,d.case_price,d.piece_price) n \n"
            + "on a.ID = n.PRODUCT_ID \n"
            + "join FORMS f \n"
            + "on a.FORM_CODE = f.CODE \n"
            + "where a.LAST_UPDATE_TIME > '{2}' and a.DOMAIN_ID = {1}", user_id, domainID, updatetime);

            DataSet ds = SqlData.OpenSql(strSql);
            return ds;
        }

        public ObjectTable GetProdListIDByProductID(int PRODUCT_ID, string domainID)
        {
            return new ObjectTable(typeof(CUSTOMER_PROD_LIST_ITEMS), string.Format("PRODUCT_ID={0} and DOMAIN_ID={1}", PRODUCT_ID, domainID));
        }

        public PRODUCTS LoadID(string ProductCode)
        {
            return (PRODUCTS)ObjectRow.Load(typeof(PRODUCTS), string.Format("this.ProductCode='{0}'", ProductCode));
        }

        public PRODUCTS LoadName(string ProductName, decimal DomainID, decimal ID)
        {
            return (PRODUCTS)ObjectRow.Load(typeof(PRODUCTS), string.Format("this.ProductName='{0}' AND this.DOMAINID='{1}' and this.ID<>'{2}'", ProductName, DomainID, ID));
        }

        public PRODUCTS LoadCode(string ProductCode, decimal Domainid)
        {
            return (PRODUCTS)ObjectRow.Load(typeof(PRODUCTS), string.Format("this.ProductCode='{0}' AND this.DOMAINID='{1}'", ProductCode, Domainid));
        }

        //绑定所有产品
        public static ObjectTable GetProducts(decimal domainid)
        {
            return new ObjectTable(typeof(PRODUCTS), "this.DOMAINID='" + domainid + "'");
        }

        public static ObjectTable GetProductwithBrand(string BrandCode, decimal domainID)
        {
            return new ObjectTable(typeof(PRODUCTS), string.Format("this.BrandCode='{0}' and this.DOMAINID='{1}'", BrandCode, domainID));
        }

        public static DataSet GetProductDynamic(decimal CustomerID, string Products, string CategoryCode, string BrandCode, string ManuCode, string FormCode, decimal domainid, string isValid, string isFirst)
        {
            return Jumpton.Data.SqlData.OpenSP("[SP_SELECT_PRODUCT_DYNAMIC]", CustomerID, Products, CategoryCode,
                                                                         BrandCode,
                                                                         ManuCode,
                                                                         FormCode,
                                                                         domainid,
                                                                         isValid,
                                                                         isFirst);
        }

    
        public static DataSet GetProductDynamicForJumpton(string Products, string CategoryCode, string BrandCode, string ManuCode, string FormCode, decimal domainid, string isValid, string isFirst)
        {
            return Jumpton.Data.SqlData.OpenSP("[SP_SelectProductDynamic]", Products, CategoryCode,
                                                                         BrandCode,
                                                                         ManuCode,
                                                                         FormCode,
                                                                         domainid,
                                                                         isValid,
                                                                         isFirst);
        }


        public static ObjectTable GetProductwithBrand(string BrandCode)
        {
            return new ObjectTable(typeof(PRODUCTS), string.Format("this.BrandCode='{0}'", BrandCode));
        }

        public static void UpdateWareHouseProduct(bool selectFlag,
                                                    decimal wareHouseProductID,
                                                    decimal productID,
                                                    string WareHouseCode,
                                                    string userCode,
                                                    string IsOnHold,
                                                    decimal domainID)
        {
            Jumpton.Data.SqlData.OpenSP("sp_UpdateWarehouseProduct",
                                            selectFlag,
                                            wareHouseProductID,
                                            productID,
                                            WareHouseCode,
                                            userCode,
                                            IsOnHold,
                                            domainID);
        }

        public static DataSet GetWareHouseProducts(string wareHouseCode,
                                                    string CategoryCode,
                                                    string ManuCode,
                                                    string BrandCode,
                                                    decimal domainid,
                                                    string isSelected)
        {
            return Jumpton.Data.SqlData.OpenSP("sp_SelectWareHouseProductList", wareHouseCode,
                                                                                CategoryCode,
                                                                                ManuCode,
                                                                                BrandCode,
                                                                                domainid,
                                                                                isSelected);
        }

        public PRODUCTS Load(string code, decimal domainID, decimal ID)
        {
            return (PRODUCTS)ObjectRow.Load(typeof(PRODUCTS), string.Format("this.ProductCode='{0}' and this.DOMAINID={1} and this.ID<>'{2}'", code, domainID, ID));
        }


        /// <summary>
        /// 保存产品信息
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public bool Save(PRODUCTS product)
        {
            try
            {

                if (product.State == DataRowState.Added)
                {
                    if (Load(product.ProductCode, product.DOMAINID, product.ID) != null)
                    {
                        throw new Exception("代码已存在.");
                    }

                    if (String.IsNullOrEmpty(product.ProductCode))
                        product.ProductCode = product.ID.ToString();

                }
                product.Save();

                return true;
            }
            catch (Exception ex)
            {
                this.message = ex.Message;
                throw ex;
            }
           
        }
    }
}
